package com.shire.shire_interface.controller.kf;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.common.utils.RandomGen;
import com.shire.shire_interface.common.utils.StrUtils;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.cs.WxChat;
import com.shire.shire_interface.model.cs.WxChatExample;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.friend.WxFriendExample;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupInvite;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.account.WxAccountService;
import com.shire.shire_interface.service.chat.WxChatService;
import com.shire.shire_interface.service.commandcenter.BaseCommandProcessor;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.friend.WxFriendService;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.group.impl.WxGroupInviteService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

//@Api(tags = "聊天接口")
@RequestMapping("/chat")
@RestController
public class KfChatController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(KfChatController.class);

    @Resource
    private WxChatService chatService;
    @Resource
    private WxAccountService accountService;
    @Resource
    private WxFriendService wxFriendService;
    @Resource
    private WxGroupService groupService;
    @Resource
    private WxGroupInviteService groupInviteService;
    @Resource
    private WxCommandService commandService;
    @Resource
    private BaseCommandProcessor commandProcessor;


    //退出群聊
    @PostMapping("/quitGroup")
    public Response quitGroup(HttpSession session, @RequestBody JSONObject jsonObject){
        Response response = new Response();

        try {
            Integer csId  = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            String groupNo = jsonObject.getString("groupNo");
            if (StringUtils.isBlank(groupNo)|| StringUtils.isBlank(wxAccount)){
                response.error("参数错误");
                return response;
            }

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("groupWxId", groupNo);
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),
                    account.getId(),
                    reqObj.toJSONString(),
                    WxConstants.CmdTaskType.UNKNOWN,
                    -1,
                    WxConstants.CmdType.CRK_QUIT_GROUP_D,
                    WxConstants.CmdPriority.UNKNOWN);

            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(), command);
            response.success("发送成功");

        }catch (Exception e) {
            e.printStackTrace();
            response.error("发生错误:" + e.getMessage());
        }
        return response;
    }

    //更新群公告
    @PostMapping("/updateGroupDesc")
    public Response updateGroupDesc(HttpSession session, @RequestBody JSONObject jsonObject){
        Response response = new Response();

        try {
            Integer csId  = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            String content = jsonObject.getString("content");
            String groupNo = jsonObject.getString("groupNo");
            if (StringUtils.isBlank(content) || StringUtils.isBlank(groupNo)|| StringUtils.isBlank(wxAccount)){
                response.error("参数错误");
                return response;
            }

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("groupWxId", groupNo);
            reqObj.put("desc", content);
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),
                    account.getId(),
                    reqObj.toJSONString(),
                    WxConstants.CmdTaskType.UNKNOWN,
                    -1,
                    WxConstants.CmdType.CRK_UPDATE_GROUP_DESC_D,
                    WxConstants.CmdPriority.UNKNOWN);

            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(), command);
            response.success("发送成功");

        }catch (Exception e) {
            e.printStackTrace();
            response.error("发生错误:" + e.getMessage());
        }
        return response;
    }
    //更新群昵称
    @PostMapping("/updateGroupNick")
    public Response updateGroupNick(HttpSession session, @RequestBody JSONObject jsonObject){
        Response response = new Response();

        try {
            Integer csId  = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            String groupNO = jsonObject.getString("groupNo");
            String groupNick = jsonObject.getString("groupNick");
            if (StringUtils.isBlank(groupNick) || StringUtils.isBlank(groupNO)|| StringUtils.isBlank(wxAccount)){
                response.error("参数错误");
                return response;
            }

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("groupWxId", groupNO);
            reqObj.put("displayName", groupNick);
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),
                    account.getId(),
                    reqObj.toJSONString(),
                    WxConstants.CmdTaskType.UNKNOWN,
                    -1,
                    WxConstants.CmdType.CRK_UPDATE_GROUP_SELF_DISPLAY_NAME_D,
                    WxConstants.CmdPriority.UNKNOWN);

            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(), command);
            response.success("发送成功");

        }catch (Exception e) {
            e.printStackTrace();
            response.error("发生错误:" + e.getMessage());
        }
        return response;
    }

    //更新群名称
    @PostMapping("/setGroupName")
    public Response setGroupName(HttpSession session, @RequestBody JSONObject jsonObject){
        Response response = new Response();

        try {
            Integer csId  = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            String groupNo = jsonObject.getString("groupNo");
            String groupName = jsonObject.getString("groupName");
            if (StringUtils.isBlank(groupNo) || StringUtils.isBlank(groupName)|| StringUtils.isBlank(wxAccount)){
                response.error("参数错误");
                return response;
            }

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("groupWxId", groupNo);
            reqObj.put("groupName", groupName);
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),
                    account.getId(),
                    reqObj.toJSONString(),
                    WxConstants.CmdTaskType.UNKNOWN,
                    -1,
                    WxConstants.CmdType.CRK_SET_GROUP_NAME_D,
                    WxConstants.CmdPriority.UNKNOWN);

            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(), command);
            response.success("发送成功");

        }catch (Exception e) {
            e.printStackTrace();
            response.error("发生错误:" + e.getMessage());
        }
        return response;
    }

    //撤回消息
    @PostMapping("/revokeMsg")
    public Response revokeMsg(HttpSession session, @RequestBody JSONObject jsonObject){
        Response response = new Response();

        try {
            Integer csId  = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            Integer chatId = jsonObject.getInteger("chatId");
            if (StringUtils.isBlank(wxAccount)|| chatId == null || chatId < 1){
                response.error("参数错误");
                return response;
            }

            WxChat chat = chatService.selectByPrimaryKey(chatId);
            if (chat.getLocalid() == null){
                response.error("此消息不可撤回");
                return response;
            }

            if(((new Date()).getTime() - chat.getCreatetime().getTime()) / 1000 > 90){
                response.error("已超进，不可撤回");
                return response;
            }

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("userWxId", chat.getFriendwxid());
            reqObj.put("localId", chat.getLocalid());
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),
                    account.getId(),
                    reqObj.toJSONString(),
                    WxConstants.CmdTaskType.UNKNOWN,
                    -1,
                    WxConstants.CmdType.CRK_REVOKE_MESSAGE_D,
                    WxConstants.CmdPriority.UNKNOWN);

            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(), command);
            response.success("发送成功");

        }catch (Exception e) {
            e.printStackTrace();
            response.error("发生错误:" + e.getMessage());
        }
        return response;
    }

//    @ApiOperation(value = "获取用户聊天信息列表")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "int", name = "csId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "accountWxId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "friendWxid"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "int", name = "pageNo"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "int", name = "pageSize")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/list_chats")
    public Response loadFriends(HttpServletRequest request,
                                @RequestParam(value = "csId") Integer csId,
                                @RequestParam(value = "accountWxId") String accountWxId,
                                @RequestParam(value = "friendWxid") String friendWxid,
                                @RequestParam(value = "pageNo") int pageNo,
                                @RequestParam(value = "pageSize") int pageSize,
                                HttpSession session) {
        Response response = new Response();

        if (!checkLoginUserByCsId(session, csId)){
            response.error("请登录！");
            response.setCode(501);
            return response;
        }

        if (friendWxid == null) {
            response.error("friendId为空！");
            return response;
        }
        List<WxChat> list = chatService.getFriendChatList(csId, accountWxId, friendWxid, getPageStartRow(pageNo, pageSize), pageSize);
        response.success("成功");
        Collections.reverse(list);
        response.setData(list);


        return response;
    }

//    @ApiOperation(value = "移除会话")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "int", name = "csId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "accountWxId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "friendWxid")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/remove_chat_list")
    public Response removeChatList(Integer csId, String accountWxId, String friendWxid, HttpServletRequest request, HttpSession session) {
        //修改最近一次消息的状态
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            if (StringUtils.isBlank(friendWxid)) {
                response.error("参数错误");
            }
            chatService.setChatShowStatusNo(csId, accountWxId, friendWxid);
            response.success("操作成功");
        } catch (Exception e) {
            logger.error("删除聊天列表异常[friendId=" + friendWxid + "]：", e);
        }
        return response;
    }

//    @ApiOperation(value = "移除群成员, post body中以json对像方式传参")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "int", name = "csId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "accountWxId"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "groupNo"),
//            @ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "memberWxId")})
//
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/removeGroupMember")
    public Response removeGroupMember(@RequestBody JSONObject bodyObject, HttpSession session){
        Response response = new Response();
        Integer csId = bodyObject.getInteger("csId");

        if (!checkLoginUserByCsId(session, csId)){
            response.error("请登录！");
            response.setCode(501);
            return response;
        }

        String accountWxid = bodyObject.getString("accountWxId");
        String groupNo = bodyObject.getString("groupNo");
        String memberWxId = bodyObject.getString("memberWxId");

        WxAccount account = accountService.getWxAccountByAccountName(csId, accountWxid);
        JSONObject cmdJson = new JSONObject();
        cmdJson.put("groupWxId",groupNo);
        cmdJson.put("userWxIds",memberWxId);
        WxCommand wxCommand = commandService.generateCmdObject(getCurrentUser(session).getId(),account.getId(),cmdJson.toJSONString(),WxConstants.CmdTaskType.UNKNOWN, -1, WxConstants.CmdType.CRK_REMOVE_GROUP_USERS_D,WxConstants.CmdPriority.UNKNOWN);
        commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(),wxCommand);
        groupService.deteleMember(csId,accountWxid,groupNo,memberWxId);
        response.success("删除成功");
        return response;
    }


//    @ApiOperation(value = "扫描图片中二维码, 参数以json格式，进行post, 需要chatId， messsage, csId")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "int", name = "body")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/scan_image")
    public Response scan_image(@RequestBody String body, HttpServletRequest request, HttpSession session) {
        JSONObject jsonBody = JSONObject.parseObject(body);
        Integer chatId = jsonBody.getInteger("chatId");
        String message = jsonBody.getString("message");
        Integer csId = jsonBody.getInteger("csId");
        Response response = new Response();

        try {
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            if (chatId == null || chatId <= 0) {
                response.error("参数错误");
            }

            boolean rst = chatService.scanImage(csId, chatId, message);
            if (rst) {
                response.success("发送成功");
            } else {
                response.success("发送失败");
            }
        } catch (Exception e) {
            logger.error("模拟识别图片异常[chatId=" + chatId + "]：", e);
        }
        return response;
    }

//    @ApiOperation(value = "接收名片加好友,参数以json格式，进行post, 需要chatId， messsage, csId")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "int", name = "body")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/accept_card")
    public Response accept_card(@RequestBody String body, HttpServletRequest request, HttpSession session) {
        JSONObject jsonBody = JSONObject.parseObject(body);
        Integer chatId = jsonBody.getInteger("chatId");
        String message = jsonBody.getString("message");
        Integer csId = jsonBody.getInteger("csId");
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            if (chatId <= 0) {
                response.error("参数错误");
            }

            boolean rst = chatService.addFriendByCard(csId, chatId, message);

            if (rst) {
                response.success("发送成功");
            } else {
                response.error("发送失败");
            }
        } catch (Exception e) {
            logger.error("根据名片添加好友异常[chatId=" + chatId + "]：", e);
        }
        return response;
    }

//    @ApiOperation(value = "取得当前帐号二维码")
//    @ApiImplicitParams(value = {@ApiImplicitParam(paramType = "form", required = true, dataType = "String", name = "accountWxid")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/show_qrcode.kf")
    public Response list_showQrcode(HttpServletRequest request, HttpServletResponse response, String accountWxid, HttpSession session) throws IOException {
        WxAccount wxAccount = accountService.getWxAccountByWxId(accountWxid);

        Response resp = new Response();
        if (!checkLoginUserByCsId(session, wxAccount.getCsid())){
            resp.error("请登录！");
            resp.setCode(501);
            return resp;
        }
        if (wxAccount == null || StrUtils.isBlank(wxAccount.getQrcode())) {
            resp.error("该账号尚无二维码！");
        }
        String qrcode = wxAccount.getQrcode();
        if (StrUtils.isBlank(qrcode)) {
            resp.error("该账号未生成二维码！");
        }
        Map<String, String> rstMap = new HashMap<String, String>();
        rstMap.put("qrcode", qrcode);
        resp.setData(rstMap);
        return resp;
    }

//    @ApiOperation(value = "消息置为已读 csId, wxAccount, friendAccount")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/readChat")
    public Response readChat(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String friendAccount = jsonObject.getString("friendAccount");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            WxChat wxChat = new WxChat();
            wxChat.setReadstatus(0);

            WxChatExample wxChatExample = new WxChatExample();
            wxChatExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendAccount);
            chatService.updateByExample(wxChat, wxChatExample);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "历史记录搜索 csId, wxAccount, friendAccount， String msg")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/historyChat")
    public Response getHistoryChat(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();

        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            String friendAccount = jsonObject.getString("friendAccount");
            String msg = jsonObject.getString("msg");
            WxChatExample wxChatExample = new WxChatExample();
            wxChatExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendAccount).andContentLike(msg);
            List<WxChat> wxChats = chatService.selectByExample(wxChatExample);
            response.success("成功");
            response.setData(wxChats);
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "好友设置是否置顶&屏蔽消息 csId, wxAccount, friendAccount, isTop 1:不置顶  2:置顶, showStatus:0：不屏蔽 1：屏蔽")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/isTop-status")
    public Response isTop(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            String friendAccount = jsonObject.getString("friendAccount");
            Integer isTop = jsonObject.getInteger("isTop");
            Integer showStatus = jsonObject.getInteger("showStatus");

            if (isTop != null) {
                WxFriend friend = new WxFriend();
                friend.setIstop(isTop);
                WxFriendExample friendExample = new WxFriendExample();
                friendExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendAccount);
                wxFriendService.updateByExampleSelective(friend, friendExample);
            }

            if (showStatus != null) {
                WxChat wxChat = new WxChat();
                wxChat.setShowstatus(showStatus);
                WxChatExample wxChatExample = new WxChatExample();
                wxChatExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendAccount);
                chatService.updateByExampleSelective(wxChat, wxChatExample);
            }

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "点击群卡片，参数，wxAccount, msgId, friendWxId")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/joinGroup")
    public Response joinGroup(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try{
            String accountWxId = jsonObject.getString("wxAccount");
            String msgId = jsonObject.getString("msgId");
            String friendWxId = jsonObject.getString("friendWxId");
            Integer chatId = jsonObject.getInteger("chatId");

            WxAccount account = accountService.getWxAccountByWxId(accountWxId);
            if (!checkLoginUserByCsId(session, account.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            JSONObject params = new JSONObject();
            params.put("groupMsgId", msgId);
            params.put("wxId", friendWxId);
            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),account.getId(),params.toJSONString(),WxConstants.CmdTaskType.UNKNOWN,-1, WxConstants.CmdType.CRK_JOIN_GROUP,WxConstants.CmdPriority.UNKNOWN);
            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(),command);

            WxChat chat = chatService.selectByPrimaryKey(chatId);
            chat.setCmdid(command.getId());
            chat.setSendstatus(command.getRststatus());
            chatService.updateChat(chat);
            response.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }


    /**
     * 邀请好友进群
     * @return
     */
//    @ApiOperation(value = "邀请好友进群,参数以json格式，进行post,参数 wxAccount, csId, friendWxid, groupNos(以逗号分开的groupNo)")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "int", name = "body")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/invite_groups")
    public Response invite_groups(@RequestBody JSONObject jsonObject, HttpSession session){
        Integer csId = jsonObject.getInteger("csId");
        String accountWxid = jsonObject.getString("wxAccount");
        String friendWxid = jsonObject.getString("friendWxid");
        String groupNos = jsonObject.getString("groupNos");

        Response response = new Response();
        if (!checkLoginUserByCsId(session, csId)){
            response.error("请登录！");
            response.setCode(501);
            return response;
        }
        if(friendWxid==null || friendWxid=="" || StrUtils.isBlank(groupNos)){
            response.error("friendAccount为空！");
            return response;
        }

        WxFriend wxFriend = wxFriendService.getFriendByWxId(csId,accountWxid,friendWxid);
        if(wxFriend==null){
            response.error("好友信息不存在！");
            return response;
        }
        WxAccount wxAccount = accountService.getWxAccountByAccountName(csId,accountWxid);
        if(wxAccount==null){
            response.error("账号信息不存在！");
            return response;
        }
        String[] eles = groupNos.split(",");

//        int maxNumPerTimes = 10;
//        int maxNumPerDay = systemVariablesService.getVariablesIntValue("invite.group.day.max.num", 30);
//        if(eles.length>maxNumPerTimes){
//            return responseFailure("每次邀请群数量不能超过"+maxNumPerTimes+"个");
//        }
        // 判断当天拉群总次数
//        long todayTotal = wxGroupInviteService.getInvitedGroupNumByAccountToday(wxAccount.getAccount());
//        if((todayTotal+eles.length)>maxNumPerDay){
//            return responseFailure("当天最大邀请数量不能超过["+maxNumPerDay+"]，当前账号已经邀请过["+todayTotal+"]，本次邀请数量["+eles.length+"]");
//        }

        for(String ele : eles){
            String groupId = ele;
            WxGroup wxGroup =groupService.selectGroupByNoForCs(csId, accountWxid,groupId);
            if(wxGroup==null) continue;
//            if(StrUtils.getNullStr(wxAccount.getBaseGroupName()).equalsIgnoreCase(wxGroup.getGroupName())){
//                continue;
//            }
//            if(wxAccountService.isBaseGroup(wxGroup.getGroupName())){
//                continue;
//            }
            WxGroupInvite wxGroupInvite = groupInviteService.genInviteGroupCmd(csId,  wxAccount, wxFriend, wxGroup);
            if(wxGroupInvite!=null) {
                chatService.genNoticeChat(wxGroupInvite.getCmdid(), RandomGen.getRandomString(20), wxFriend,
                        "发送群邀请["+wxGroup.getGroupname()+"]", csId);
            }
        }
        response.success("发送成功！");
        return response;
    }
}
